import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)


const module = {
    state: {
        user: {
            name: '',
            id: '',
            tenantId: '',
            accessToken: '',
            ticket: '',
            courseId: 0,
            rate: 0,
            refresh: true
        }
    },
    getters: {},
    mutations: {
        setUser(state, payload) {
            if (payload.hasOwnProperty('name')) {
                state.user.name = payload.name
            }
            if (payload.hasOwnProperty('id')) {
                state.user.id = payload.id
            }
            if (payload.hasOwnProperty('tenantId')) {
                state.user.tenantId = payload.tenantId
            }
            if (payload.hasOwnProperty('accessToken')) {
                state.user.accessToken = payload.accessToken
            }
            if (payload.hasOwnProperty('ticket')) {
                state.user.ticket = payload.ticket
            }
            if (payload.hasOwnProperty('courseId')) {
                state.user.courseId = payload.courseId
            }
            if (payload.hasOwnProperty('rate')) {
                state.user.rate = payload.rate
            }
            if (payload.hasOwnProperty('refresh')) {
                state.user.refresh = payload.refresh
            }
        },
        LOGOUT(state) {
            sessionStorage.clear();
            state.user.name = '';
            state.user.id = '';
            state.user.tenantId = '';
            state.user.ticket = '';
            state.user.accessToken = '';
            state.user.courseId = 0;
            state.user.rate = 0;
            state.user.refresh = true;
            state.user = {};
        },
    },
    plugins: [createPersistedState()]
}


const store = new Vuex.Store(module)
export default store